import {
  listFood
} from "../../api/index/food"
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 是否显示面板指示点
    indicatorDots: true,
    // 自动播放
    autoplay: false,
    // 自动切换时间间隔
    interval: 5000,
    // 滑动动画时长
    duration: 500,
    // 当前轮播图片
    curSwiper: 0,
    // 轮播图
    swiperImageList: [{
        id: 1,
        image: "http://192.168.0.196:8000/profile/swiper/1.jpg"
      },
      {
        id: 2,
        image: "http://192.168.0.196:8000/profile/swiper/2.jpg"
      },
      {
        id: 3,
        image: "http://192.168.0.196:8000/profile/swiper/3.jpg"
      },
      {
        id: 4,
        image: "http://192.168.0.196:8000/profile/swiper/4.jpg"
      },
      {
        id: 5,
        image: "http://192.168.0.196:8000/profile/swiper/5.jpg"
      },
    ],
    // 营业状态
    workStatus: true,
    // 菜品列表
    foodList: [],
    // 总共菜品
    total: 0
  },

  onLoad() {
    this.getFoodList();
  },

  onShow() {
    this.updateTabBarIndex();
  },

  // 请求菜品列表
  getFoodList() {
    listFood({
      pageNum: 1,
      pageSize: 10
    }).then(res => {
      res.rows.forEach(item => {
        item.img = app.globalData.resourceUrl + item.image
      })
      this.setData({
        foodList: res.rows,
        total: res.total
      })
    }).catch(err => {
      console.error(err);
    })
  },

  updateTabBarIndex() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },

  // 切换轮播图
  cardSwiperChange(e) {
    this.setData({
      curSwiper: e.detail.current
    });
  },

  // 预览图片
  previewFoodImage(e) {
    const cur = e.target.dataset.cur;
    const imageList = this.data.foodList.map(item => item.img);
    wx.previewImage({
      current: cur,
      urls: imageList
    })
  },

  // 前往菜品详情
  goFoodDetail(e) {
    const dataset = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/food-detail/food-detail?id=' + dataset.id,
    })
  }
})